/**
 * Created by Amaris on 2016/12/6.
 */


var util = require('../utils/util.js');
var daoUtil = require('../utils/daoUtil');
var uuid = require('node-uuid');

var addressbookDeptClass = require('../class/addressbookDept.js');

module.exports = {

    create: function (params, cb,addressbook) {
        var validate = daoUtil.validate(['name', 'createUid', 'enable'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var addressbookDept = new addressbookDeptClass();
            addressbookDept.init(params);
            models.addressbookdept.find({"name":addressbookDept.name}).exec(function (err, data){
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    if(data.length >0 ){
                        cb(null, util.getSuccessRes(data[0]));
                        return;
                    }
                    models.addressbookdept.create(addressbookDept.getParams(), function (err, data) {
                        if (err || !data) {
                            console.error(err);
                            cb(util.getSysError());
                        }else{
                            cb(null, util.getSuccessRes(data));
                        }
                    });
                }
            })
        },cb);
    },

    //按名称获取角色列表
    getListByName: function (params, cb) {
        var validate = daoUtil.validate(['query'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.addressbookdept.find({name:{ 'like': '%'+params.query+'%' }}).sort('createdAt DESC').limit(5).exec(function (err, data){
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    }

};



